package com.example.cloud.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;

@RestController
public class GreetAction {

    @Autowired
    private EurekaClient eurekaClient;
    @Autowired
    private GreetClient greetClient;
    @Autowired
    DiscoveryClient discoveryClient;

    @RequestMapping("/greet")
    public String greet() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("grpc-service", false);
            sb.append(instanceInfo.getIPAddr() + ":" + instanceInfo.getPort() + "<br/>");
        }
        greetClient.greet();
        return sb.toString();
    }

}
